#include "SourceDao.h"

SourceDao::SourceDao(void)
{
	this->db=DB::getInstance();
}

SourceDao::~SourceDao(void)
{
}

list<Element*>* SourceDao::findAll(void)
{
	list<Element *> * result = new list<Element *>();
	map<string,Element *>::iterator iter;
	for(iter = this->db->getSources()->begin() ; iter != this->db->getSources()->end() ; ++iter) {
		result->push_front(iter->second);
	}
	return result;
}

void SourceDao::save(Element *object)
{
	try {
		this->find(object->getId());
		throw PersistenceUniqueConstraintException("Se intento guardar un elemento duplicado.");
	}catch(NoSuchElementException e) {
		this->db->getSources()->insert(make_pair(object->getId(),object));
	}	
}

Element* SourceDao::find(string id)
{
	map<string,Element *>::iterator iter = db->getSources()->find(id);
	if(iter != db->getSources()->end()) {
		return iter->second;
	}
	throw NoSuchElementException("No existe el elemento con ese id");
}

void SourceDao::deleteObject(Element *objectToDelete)
{
	return;
}